package xyz.xwls.bootadmin.sys.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xyz.xwls.bootadmin.sys.domain.Role;
import xyz.xwls.bootadmin.sys.repository.RoleRepository;
import xyz.xwls.bootadmin.sys.service.RoleService;
import xyz.xwls.bootadmin.sys.vo.RoleVo;
import xyz.xwls.bootadmin.util.UpdateUtil;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author WZW
 * @date 2020/4/14
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleRepository roleRepository;

    @Override
    public List<RoleVo> list() {
        List<Role> roleList = roleRepository.findAll();
        return convert(roleList);
    }

    @Override
    public Role save(Role role) {
        return roleRepository.save(role);
    }

    @Override
    public Role update(Role role) {
        Role source = roleRepository.getOne(role.getId());
        UpdateUtil.copyNullProperties(source, role);
        return roleRepository.save(role);
    }

    public static RoleVo convert(Role role) {
        RoleVo roleVo = new RoleVo();
        BeanUtils.copyProperties(role, roleVo);
        return roleVo;
    }

    public static List<RoleVo> convert(List<Role> roleList) {
        return roleList.stream().map(RoleServiceImpl::convert).collect(Collectors.toList());
    }
}
